<?php
class PetProfileController extends Model3_Controller
{
    public function init()
    {
        $credentials = Model3_Auth::getCredentials();
        
        $this->view->id_user = $credentials['idUsuario'];
    }

    
    protected function indexAction()
    {
        $petsAdapter = MN_PetsTable::getInstance();
        $albumAdapter = MN_PetsAlbumTable::getInstance();
        $petToSwho = false;
        $images = false;
        if(count($this->getRequest()->getParams())>0)
        {
            $petId = $this->getRequest()->getParam('petId');
            $pet = $petsAdapter->getById($petId);
            $images = $albumAdapter->getByPetId($petId);
            if($pet instanceof MN_Pets)
            {
                $petToSwho = $pet;
            }
        }
        $this->view->images = $images;
        $this->view->pet = $petToSwho;
    }
    
    public function createPetProfileAction()
    {
        $clase = '';
        $message = '';
        if($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            
            $pet = new MN_Pets;
            $date = new DateTime();
            
            $pet->name = $post['nombre'];
            $pet->type = $post['type'];
            $pet->age = $post['age'];
            $pet->sex = $post['sex'];
            // $user->password = 
            $pet->status = $post['status'];
            $pet->pedigree = $post['pedigree'];
            $pet->description = $post['description'];
            // $user->breed = $post['description'];
            $pet->color = $post['color'];
            $pet->price = $post['price'];
            $pet->weigth = $post['peso'];
            $pet->large_description = $post['description-large'];
            $pet->creation_date = $date->format('Y-m-d H:i:s');
            $pet->owner_id = $this->view->id_user;
            
            
            if($pet->trySave())
            {
                // importar la imagen
                $pet->image = $this->loadPicture($pet);
                $pet->trySave();
                // enviar a compartir en redes
                $this->redirect('PetProfile/share');
            }
        }
    }
    
    public function addPictureAction()
    {
        $idPet = '';
        if($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $petsAdapter = MN_PetsTable::getInstance();
            $pet = $petsAdapter->getById($post['pet']);
            
            if($pet instanceof MN_Pets)
            {
                $idPet = $pet->id;
                $picture = new MN_PetsAlbum;
                $date = new DateTime();

                // importar la imagen
                $picture->id_pet = $pet->id;
                $picture->status = 1;
                $picture->image = $this->loadPicture($pet);
                $picture->comment = $post['comment-pic'];
                $picture->creation_date = $date->format('Y-m-d H:i:s');
                $picture->trySave();
            }
        }
        
        $this->redirect('PetProfile/index/petId/'.$idPet.'/');
    }
    
    private function loadPicture(MN_Pets $pet)
    {
        $path = '';
        $date = new DateTime();
        $this->prepareServerToUpload();
        $types = array(Helper_UploadFile::TYPE_IMAGES);
        $dirSave = 'uploads/petsprofile';
        $uploadFile = new Helper_UploadFile($types, Helper_UploadFile::SIZE_3MB, $dirSave);

        $newName = $pet->id.$date->format('YmdHis').'_pet-profile';

        $ret = $uploadFile->uploadIndividualFile($_FILES['archivos'], $newName);
        if($ret['result'])
        {
                // la imagen del producto
                $path = $imageOriginal = $ret['path'];
                $resizeImage = new Model3_Img($imageOriginal);
                $resizeImage->redimensionarImagen(640, 480, $imageOriginal);
        }
        return $path;
    }
    
    public function listAction()
    {
        $petsAdapter = MN_PetsTable::getInstance();
        
        $pets = $petsAdapter->getMyPets($this->view->id_user);
        
        $this->view->pets = $pets;
    }
    
    public function shareAction()
    {
        
    }
     
    private function prepareServerToUpload()
    {
        set_time_limit(360000);
        ini_set('memory_limit', '3000M');
        ini_set('max_execution_time', 6000);
        ini_set('max_input_time', 3600);
    }

}
